package com.example.http.chapter6;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 通过用的Http模块测试
 *
 * @author zhouxl
 * @DATE 2021/1/22
 * @descriptoin
 */
@RestController
@RequestMapping("/api/httpCommon")
public class HttpCommon {

    /**
     * 测试http的via字段信息
     * 测试了get和patch方法，没有效果
     *
     * @param request
     * @param response
     * @return void
     * @author zhouxl
     * @date 2021-01-22
     */
    @RequestMapping("/via")
    public void via(HttpServletRequest request, HttpServletResponse response) {
        // org.apache.catalina.connector.RequestFacade@64548dbb 为什么会出现一个java类的信息在header里面
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            System.out.println(name + ":" + request.getHeader(name));
        }
    }

    /**
     * 返回期望的格式数据
     *
     * @param request
     * @param response
     */
    @RequestMapping("/accept1")
    public String accept1(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/plain");
        return "http字段属性accept";
    }

    /**
     * 返回非期望的格式数据
     *
     * @param request
     * @param response
     */
    @RequestMapping("/accept2")
    public String accept2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        FileInputStream fis = new FileInputStream("D:\\springboot-example\\springboot-example\\springboot-http\\src\\main\\resources\\static\\2.jpg");
        FileInputStream fis2 = new FileInputStream("D:\\springboot-example\\springboot-example\\springboot-http\\src\\main\\resources\\static\\2.jpg");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        IOUtils.copy(fis, bos);
        byte[] bytes = bos.toByteArray();
//        System.out.println(Arrays.toString(bytes));
        String encode = new BASE64Encoder().encode(bytes);
        System.out.println(encode);
        System.out.println("----------------------------------------------------------------------------------");

        //        response.setContentType("text/plain");
//        response.setContentType("text/plain");
        IOUtils.copy(fis2, response.getOutputStream());
        response.getOutputStream().flush();
        fis2.close();
        return null;
    }

    /**
     * 测试http的authorzation字段信息
     *
     * @param response
     * @return java.lang.String
     * @author zhouxl
     * @date 2021-01-22
     */
    @RequestMapping("/authorzation")
    public String authorzation(HttpServletResponse response) {
        response.setHeader("WWW-Authenticate", "Basic dWVub3ksjkjDskdj35DS");
        return "authorzation";
    }

    /**
     * 测试http的From字段信息
     *
     * @param response
     * @return java.lang.String
     * @author zhouxl
     * @date 2021-01-22
     */
    @RequestMapping("/from")
    public String from(HttpServletResponse response) {
        response.setHeader("From", "ssfxxaa@qq.com");
        return "from";
    }

    /**
     * 测试http的match字段信息
     *
     * @param response
     * @return java.lang.String
     * @author zhouxl
     * @date 2021-01-22
     */
    @RequestMapping("/match")
    public String match(HttpServletResponse response) {
        return "from";
    }

}
